<?php

namespace App\Jobs;

use App\Helpers\CommTrait;
use App\Helpers\NewBaseData;
use App\Http\Controllers\Wx\SendTemplateController;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\Service;
use Modules\Manager\Entities\Manager;

class PreArrangeCar implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels,NewBaseData,CommTrait;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $order;
    public function __construct()
    {
        //
       // $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order = CarOrder::query()
            ->where('status',1)
            ->where('create_order_category',3)
            ->where('pre_fetch_time','<',now()->addHours(3))
            ->get();
        if($order){
            foreach ($order as $item){
                if($item->status==0){
                    $item->status = -3;
                    if($item->is_coupon==1){
                        $this->recoupon($item->coupon_id);
                    }
                }
                $item->save();
            }
        }
    }
    // 处理还剩三个小时 发车数据
    protected function getLastThreeHours(){
        $orders = CarOrder::query()
            ->where('status',1)
            ->where('create_order_category',3)
            ->whereNotNull('fetch_time')
            ->where('pre_fetch_time','<',now()->addHours(3))
            ->get();
        if($orders){
            $template = new SendTemplateController();
            foreach ($orders as $order){
                // 发送模板消息
                $getDepartment = $this->getManager($order->department);
                if($getDepartment->count()>0){
                    foreach ($getDepartment as $openid){
                        info('临近三小时发货通知'.$openid);
                        $template->preOrderOnTime($openid,$order);
                    }
                }

            }
        }
    }
    // 处理临近2小时还车订单
    protected function getLastTwoHours(){
        $orders = CarOrder::query()
            ->where('status',4)
            ->whereNotNull('fetch_time')
            ->where('pre_return_time','<',now()->addHours(3))
            ->get();
        if($orders){
            $template = new SendTemplateController();
            foreach ($orders as $order){
                // 发送模板消息
                $getDepartment = $this->getManager($order->department);
                if($getDepartment->count()>0){
                    foreach ($getDepartment as $openid){
                        info('临近三小时发货通知'.$openid);
                        $template->preOrderOnTime($openid,$order);
                    }
                }

            }
        }
    }
    // 订单超过两个小时未回收
    protected function orderOverTwoHours(){
        $orders = CarOrder::query()
            ->where('status',4)
            ->whereNotNull('fetch_time')
            ->where('pre_return_time','<',now()->addHours(2))
            ->get();
        if($orders){
            $template = new SendTemplateController();
            foreach ($orders as $order){
                // 发送模板消息
                $getDepartment = $this->getManager($order->department);
                if($getDepartment->count()>0){
                    foreach ($getDepartment as $openid){
                        info('续租提醒'.$openid);
                        $template->orderOverTime($openid,$order);
                    }
                }

            }
        }
    }
    protected function getOrderType($type){
        switch ($type){
            case 1:
                return '小程序订单';
            case 2:
                return '立即出车';
            case 3:
                return '预订单';
        }
    }

}
